/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta.components.category;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Jan Korreck
 */
public class CategoryManager {

    private static CategoryManager INSTANCE = new CategoryManager();


    private HashMap<String, Category> allCats = new HashMap<String, Category>();

    public static CategoryManager getInstance() {
        return INSTANCE;
    }

    public void addCategory(Category newCat) {

        System.out.println("BEFORE: " + this.allCats.keySet().size() + "(" + newCat.getName() + ")");
        if (!this.allCats.containsKey(newCat.getName())) {
            this.allCats.put(newCat.getName(), newCat);
        }
        System.out.println("AFTER: " + this.allCats.keySet().size());
    }

    public void removeCategory(Category newCat) {
        this.allCats.remove(newCat.getName());
    }

    public void removeCategory(String id) {
        if (allCats.containsKey(id)) {
            this.allCats.remove(id);
        }
    }

    public Category getCategory(String name) {
        return this.allCats.get(name);
    }

    public boolean hasCategory(String catID) {
        return this.allCats.containsKey(catID);
    }

    public List<Category> getAllCategories() {
        return new ArrayList(this.allCats.values());
    }

    public List<String> getCategoryNames() {
        ArrayList<String> names = new ArrayList<String>();


        Iterator<Category> valIt = this.allCats.values().iterator();

        while(valIt.hasNext()) {
            names.add(valIt.next().getName());
        }

        return names;
    }

    public int getSize() {
        return this.allCats.size();
    }

    public List<Category> find(int first, int count, String sortProperty, boolean sortAsc) {

        List<Category> subList = getIndex(sortProperty, sortAsc).subList(first, first + count);


        return subList;
    }

    private List<Category> getIndex(String sortProperty, boolean sortAsc) {
        // hier müsste die DB gefragt werden und dementsprechend sortiert werden

        System.out.println("___________________________________ SortProperty: " + sortProperty);
        if (sortProperty == null) {
            return new ArrayList<Category>(this.allCats.values());

        } else if (sortProperty.equals("name")) {

            return this.sortByName(new ArrayList<Category>(this.allCats.values()), sortAsc);

        }

        throw new RuntimeException("unknown sort option [" + sortProperty +
                "]. valid options: [id] , [name]");
    }

    private List<Category> sortByName(ArrayList<Category> cats, boolean asc) {

        if (asc) {
            Collections.sort(cats, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Category)o1).getName().compareTo(((Category)o2).getName());
                }
            });
        } else {
            Collections.sort(cats, new Comparator() {

                public int compare(Object o1, Object o2) {

                    return ((Category)o2).getName().compareTo(((Category)o1).getName());
                }
            });
        }
        return cats;
    }
}
